﻿using Microsoft.Extensions.Options;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;

namespace Loong.Admin.Users
{
    public class PasswordValidator : IPasswordValidator
    {
        private readonly PasswordOptions _options;

        public PasswordValidator(IOptions<PasswordOptions> optionsAccessor)
        {
            _options = optionsAccessor.Value;
        }

        public Task ValidateAsync(string? password)
        {
            Check.NotNull(password, nameof(password));

            if (string.IsNullOrWhiteSpace(password) || password.Length < _options.RequiredLength)
            {
                throw new BusinessException(message: $"密码长度不能小于{_options.RequiredLength}");
            }

            if (_options.RequireNonAlphanumeric && password.All(IsLetterOrDigit))
            {
                throw new BusinessException(message: "密码需包含特殊字符");
            }

            if (_options.RequireDigit && !password.Any(IsDigit))
            {
                throw new BusinessException(message: "密码需包含数字");
            }

            if (_options.RequireLowercase && !password.Any(IsLower))
            {
                throw new BusinessException(message: "密码需包含小写字母");
            }

            if (_options.RequireUppercase && !password.Any(IsUpper))
            {
                throw new BusinessException(message: "密码需包含大写字母");
            }

            if (_options.RequiredUniqueChars >= 1 && password.Distinct().Count() < _options.RequiredUniqueChars)
            {
                throw new BusinessException(message: $"密码需包含至少{_options.RequiredUniqueChars}个不同的字符");
            }

            return Task.CompletedTask;
        }

        /// <summary>
        /// Returns a flag indicating whether the supplied character is a digit.
        /// </summary>
        /// <param name="c">The character to check if it is a digit.</param>
        /// <returns>True if the character is a digit, otherwise false.</returns>
        public virtual bool IsDigit(char c)
        {
            return c >= '0' && c <= '9';
        }

        /// <summary>
        /// Returns a flag indicating whether the supplied character is a lower case ASCII letter.
        /// </summary>
        /// <param name="c">The character to check if it is a lower case ASCII letter.</param>
        /// <returns>True if the character is a lower case ASCII letter, otherwise false.</returns>
        public virtual bool IsLower(char c)
        {
            return c >= 'a' && c <= 'z';
        }

        /// <summary>
        /// Returns a flag indicating whether the supplied character is an upper case ASCII letter.
        /// </summary>
        /// <param name="c">The character to check if it is an upper case ASCII letter.</param>
        /// <returns>True if the character is an upper case ASCII letter, otherwise false.</returns>
        public virtual bool IsUpper(char c)
        {
            return c >= 'A' && c <= 'Z';
        }

        /// <summary>
        /// Returns a flag indicating whether the supplied character is an ASCII letter or digit.
        /// </summary>
        /// <param name="c">The character to check if it is an ASCII letter or digit.</param>
        /// <returns>True if the character is an ASCII letter or digit, otherwise false.</returns>
        public virtual bool IsLetterOrDigit(char c)
        {
            return IsUpper(c) || IsLower(c) || IsDigit(c);
        }
    }
}
